//==========Ajax请求=============
function ajax(url,method,data,isAsync,success_callback,error_callback) {
    $.ajaxSetup( {
        beforeSend: function(xhr) {
            xhr.setRequestHeader("token", localStorage.token);
        }
    });
    $.ajax({
        url:url,
        data:data,
        method:method,
        dataType:'json',
        async:isAsync,
        success:function (result) {
            if(result.code!=0){
                if(result.code==100){
                    window.location.href="./login.html"
                }else{
                    modals.error(result.message);
                    if(error_callback) error_callback(result.message);
                }
            }else {
                if(success_callback) success_callback(result.data);
            }
        },
    })
}

function ajaxPostFile(url, params, success_callback,error_callback) {
    $.ajaxSetup( {
        beforeSend: function(xhr) {
            xhr.setRequestHeader("token", localStorage.token);
        }
    });
    $.ajax({
        type : 'post',
        url : url,
        data : params,
        contentType: false,
        processData: false,
        cache: false,
        success:function (result) {
            if(result.code!=0){
                if(result.code==100){
                    window.location.href="./login.html"
                }else{
                    modals.error(result.message);
                    if(error_callback) error_callback(result.message);
                }
            }else {
                if(success_callback) success_callback(result.data);
            }
        },
    });
}

function doPost(url, data,isAsync, success_callback,error_callback){
    ajax(url,"post",data,isAsync,success_callback,error_callback);
}

function doGet(url, data, isAsync,success_callback,error_callback){
    ajax(url,"get",data,isAsync,success_callback,error_callback);
}

function doPut(url, data,isAsync, success_callback,error_callback){
    ajax(url,"put",data,isAsync,success_callback,error_callback);
}

function doDelete(url, data, isAsync,success_callback,error_callback){
    ajax(url,"delete",data,isAsync,success_callback,error_callback);
}

//异步
function AsyncPost(url, data,success_callback,error_callback){
    doPost(url, data,true, success_callback,error_callback );
}
function AsyncGet(url, data, success_callback,error_callback){
    doGet(url, data, true,success_callback,error_callback );
}
function AsyncPut(url, data,success_callback,error_callback){
    doPut(url, data,true, success_callback,error_callback );
}
function AsyncDelete(url, data, success_callback,error_callback){
    doDelete(url, data, true,success_callback,error_callback );
}

function SyncPost(url, data, success_callback,error_callback){
    doPost(url, data,false, success_callback,error_callback );
}

function SyncGet(url, data, success_callback,error_callback){
    doGet(url, data,false, success_callback, error_callback);
}
function AsyncPut(url, data,success_callback,error_callback){
    doPut(url, data,false, success_callback,error_callback );
}
function AsyncDelete(url, data, success_callback,error_callback){
    doDelete(url, data, false,success_callback,error_callback );
}